

package com.suzj.extraenchantments.cap;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;

import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class CoolDownProvider implements ICapabilityProvider, ICapabilitySerializable<CompoundTag> {
    private final ICoolDown capability = new CoolDownStandardImpl();
    private final LazyOptional<ICoolDown> impOptional = LazyOptional.of(() -> this.capability);

    @Nonnull
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        return Capabilities.COOL_DOWN_CAPABILITY.orEmpty(cap, impOptional);
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag = new CompoundTag();
        for (var coolDownType : CoolDownType.values()) {
            tag.putInt(coolDownType.name(), capability.get(coolDownType));
        }

        return tag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {

        for (var coolDownType : CoolDownType.values()) {
            int temp = (nbt).getInt(coolDownType.name());
            capability.set(coolDownType, temp);
        }
    }
}
